package com.aegean.MonitorExamSYS.center.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.aegean.MonitorExamSYS.center.business.CenterBusiness;
import com.aegean.MonitorExaxSYS.model.MonitorValue;
import com.opensymphony.xwork2.ActionSupport;

@Controller("ConfirmArrangeForSinglerMonitor")
@Scope("prototype")
public class ConfirmArrangeForSinglerMonitor extends ActionSupport implements
		ServletRequestAware {
	@Resource
	CenterBusiness business;
	private HttpServletRequest request;
	private HttpSession session;
	private String msg;
	private boolean success;
	private Map<Integer, List<Integer>> monitorSelectedMap;// 定义每个考场的监考人员信息
	private Map<Integer, List<MonitorValue>> monitorMap;// 获取每个考试单位的监考人员信息
	private int position_Id;
	private int[] monitors_Id;
	private int unit_Id;

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public Map<Integer, List<Integer>> getMonitorSelectedMap() {
		return monitorSelectedMap;
	}

	public void setMonitorSelectedMap(Map<Integer, List<Integer>> monitorSelectedMap) {
		this.monitorSelectedMap = monitorSelectedMap;
	}

	public Map<Integer, List<MonitorValue>> getMonitorMap() {
		return monitorMap;
	}

	public void setMonitorMap(Map<Integer, List<MonitorValue>> monitorMap) {
		this.monitorMap = monitorMap;
	}

	public int getPosition_Id() {
		return position_Id;
	}

	public void setPosition_Id(int position_Id) {
		this.position_Id = position_Id;
	}

	public int[] getMonitors_Id() {
		return monitors_Id;
	}

	public void setMonitors_Id(int[] monitors_Id) {
		this.monitors_Id = monitors_Id;
	}

	public int getUnit_Id() {
		return unit_Id;
	}

	public void setUnit_Id(int unit_Id) {
		this.unit_Id = unit_Id;
	}

	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		try {
		if (session.getAttribute("MonitorSelectedMap") == null) {
			monitorSelectedMap = new HashMap<Integer, List<Integer>>();

		} else {
			monitorSelectedMap = (Map<Integer, List<Integer>>) session
					.getAttribute("MonitorSelectedMap");
			if (monitorSelectedMap.get(position_Id) == null) {
			} else {
				monitorSelectedMap.remove(position_Id);
			}
		}
		List<Integer> ids = new ArrayList<Integer>();
		for (int i = 0; i < monitors_Id.length; i++) {
			ids.add(monitors_Id[i]);
		}
		monitorSelectedMap.put(position_Id, ids);
		monitorMap = (Map<Integer, List<MonitorValue>>) session
				.getAttribute("MonitorsForMonitorApply");
		List<MonitorValue> monitors = monitorMap.get(unit_Id);
		for (int i = 0; i < monitors.size(); i++) {
			for (int j = 0; j < monitors_Id.length; j++) {
				if (monitors.get(i).getUser_Id() == monitors_Id[j]) {
					monitors.remove(i);
					break;
				}
			}
		}
		monitorMap.remove(unit_Id);
		monitorMap.put(unit_Id, monitors);
		session.setAttribute("MonitorsForMonitorApply", monitorMap);
		session.setAttribute("MonitorSelectedMap", monitorSelectedMap);
		success=true;
		msg="成功，安排该考场监考人员工程！";
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			success=false;
			msg="对不起,系统安排出错！";
		}
		return SUCCESS;
	}

	@Override
	public void setServletRequest(HttpServletRequest arg0) {
		// TODO Auto-generated method stub
		request = arg0;
		session = request.getSession();

	}
}
